package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.SortTypeDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.SortType;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @SortTypeController
 * @类别Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/sortType")
public class SortTypeController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private SortTypeDao sortTypeDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改类别
     * @param sortType
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSortType",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSortType(@RequestBody SortType sortType){
        log.debug("添加/修改类别");
        sortTypeDao.save(sortType);
        return JsonResp.ok();
    }



    /**
     * @（店铺、货物）类型接口添
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/findShopOrGoodsTypeList",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findShopOrGoodsTypeList(){
        log.debug("添加/修改类别");
        List<SortType> sortTypeList=sortTypeDao.findByType(1);
        return JsonResp.ok(sortTypeList);
    }

    /**
     * @ 查找所有商户类型
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/findAllShop",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findAllShop(){
        log.debug("查找所有商户类型");
        List<SortType> sortTypeList=sortTypeDao.findByType(1);
        return JsonResp.ok(sortTypeList);
    }





    /**
     * @批量添加/修改类别
     * @param sortTypes
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomeSortType",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomeSortType(@RequestBody List<SortType> sortTypes){
        log.debug("批量添加/修改类别");
        sortTypeDao.save(sortTypes);
        return JsonResp.ok();
    }

    /**
     * @获取类别详情-BySortTypeId
     * @param sortTypeId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/sortTypeDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findSortTypeDetailsBySortTypeId(Long sortTypeId) {
        log.debug("获取类别详情-BySortTypeId");
        return JsonResp.ok(sortTypeDao.findOne(sortTypeId));
    }

    /**
     * @分页查询类别(jpa方式)
     * @param type 类型
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageSortTypeJpa( Integer pageNum, Integer pageSize,Integer type) {
        log.debug("分页查询类别(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
        if (type == 0) {
            page = sortTypeDao.findAll(pageable); //无条件分页
        }else{
            page = sortTypeDao.findAll( 		  //条件分页
                    (root, criteriaQuery, cb) -> {
                        Predicate predicate = cb.conjunction();
                        List<Expression<Boolean>> expressions = predicate.getExpressions();
                        expressions.add(cb.equal(root.get("type"), type));
                        return predicate;
                    }, pageable);
        }
//        List<Message> content = page.getContent();
//        for (Message message1 : content) {
//        }
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除类别-one(永久)
     * @param sortTypeId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSortTypeOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSortTypeOne(Long sortTypeId) {
        log.debug("删除类别-one(永久)");
        sortTypeDao.delete(sortTypeId);
        return JsonResp.ok();
    }

    /**
     * @批量删除类别(永久)
     * @param sortTypes
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomeSortType",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomeSortType(@RequestBody List<SortType> sortTypes) {
        log.debug("批量删除类别(永久)");
        sortTypeDao.delete(sortTypes);
        return JsonResp.ok();
    }


}
